package com.adv.ems.filters.original.before;

import com.adv.ems.db.entity.CollectPointDO;
import com.adv.ems.enums.CollectPointDataTypeEnum;
import com.adv.ems.enums.CollectPointWarningTypeEnum;
import com.adv.ems.filters.EnergyDataBaseFilter;
import com.adv.ems.filters.dto.FilterParam;
import com.adv.ems.filters.original.dto.EnergyDataOriginalBeforeDTO;
import com.adv.ems.manager.CollectPointManager;
import com.adv.ems.model.vo.ResultVO;
import com.adv.ems.service.WarningService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

@Component
public class DataSafeCheckBeforeFilter extends EnergyDataBaseFilter<EnergyDataOriginalBeforeDTO, Boolean> {
    @Resource
    WarningService warningService;
    @Resource
    CollectPointManager collectPointManager;

    @Override
    protected String getFilterName() {
        return "采集点数据上下限告警";
    }

    @Override
    protected double getOrder() {
        return 2;
    }

    @Override
    protected boolean isBefore() {
        return true;
    }

    @Override
    protected String getType() {
        return EnergyDataOriginalBeforeDTO.class.getName();
    }

    @Override
    protected ResultVO<Boolean> handle(FilterParam<EnergyDataOriginalBeforeDTO> param) {
        for (EnergyDataOriginalBeforeDTO data: param.getList()) {
            CollectPointDO point = collectPointManager.getAllMap().get(data.getCollectPointId());
            if (!CollectPointDataTypeEnum.PROCESS.name().equals(point.getDataType())) {
                continue;
            }
            if (point.getMinSafeValue() == null && point.getMaxSafeValue() == null) {
                continue;
            }
            Double realValue = data.getRealValue();
            if (point.getMinSafeValue() != null && realValue < point.getMinSafeValue()) {
                String msg = String.format("采集数据真实值【%s】低于采集点预设最低告警值【%s】", realValue, point.getMinSafeValue());
                warningService.pushCollectPointWarning(data.getId(), point.getId(), CollectPointDO.class, CollectPointWarningTypeEnum.SAFE_LOW, realValue.toString(), msg);
            }
            if (point.getMaxSafeValue() != null && realValue > point.getMaxSafeValue()) {
                String msg = String.format("采集数据真实值【%s】高于采集点预设最高告警值【%s】", realValue, point.getMaxSafeValue());
                warningService.pushCollectPointWarning(data.getId(), point.getId(), CollectPointDO.class, CollectPointWarningTypeEnum.SAFE_UP, realValue.toString(), msg);
            }
        }
        return ResultVO.success();
    }
}
